package com.supcon.exchangedata.service;

import com.supcon.exchangedata.pojo.entity.oracle.CtBusRouteInfo;
import com.supcon.exchangedata.pojo.entity.mysql.operation.gj.BusRouteInfo;
import com.supcon.exchangedata.repository.master.MasterBusRouteInfoRepository;
import com.supcon.exchangedata.repository.slave.SlaveBusRouteInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class BusRouteInfoService {

    @Resource
    private MasterBusRouteInfoRepository masterBusRouteInfoRepository;

    @Resource
    private SlaveBusRouteInfoRepository slaveBusRouteInfoRepository;

    @Transactional(rollbackFor = Exception.class)
    public void transBusRouteInfo() {
        List<BusRouteInfo> all = this.slaveBusRouteInfoRepository.findAll();
        if (!CollectionUtils.isEmpty(all)) {
            List<CtBusRouteInfo> busRouteInfoList = new ArrayList<>();
            all.forEach(e -> {
                CtBusRouteInfo busRouteInfo = new CtBusRouteInfo();
                busRouteInfo.setRouteid(e.getRouteid());
                busRouteInfo.setSubrouteid(e.getSubrouteid());
                busRouteInfo.setSegmentid(e.getSegmentid());
                busRouteInfo.setRoutename(e.getRoutename());
                busRouteInfo.setRoutetype(e.getRoutetype());
                busRouteInfo.setFstsendtime(e.getFstsendtime());
                busRouteInfo.setLstsendtime(e.getLstsendtime());
                busRouteInfo.setSegmentname(e.getSegmentname());
                busRouteInfo.setSubid(e.getSubid());
                busRouteInfo.setBizTime(e.getBizTime());
                busRouteInfo.setLoadTime(e.getLoadTime());
                busRouteInfo.setSngmile(e.getSngmile());
                busRouteInfoList.add(busRouteInfo);
            });
            this.masterBusRouteInfoRepository.deleteAll();
            this.masterBusRouteInfoRepository.saveAll(busRouteInfoList);
        }
    }
}
